package com.bw.controller;

import com.bw.bean.Middle;
import com.bw.bean.Singer;
import com.bw.bean.Song;
import com.bw.service.InService;
import com.bw.uilt.FileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Icontroller {
    @Autowired
    private InService service;

    @RequestMapping("list.do")
    public String getlist(Model model,
                          @RequestParam(defaultValue = "1")Integer pageNum,
                          @RequestParam(defaultValue = "")String itime1,
                          @RequestParam(defaultValue = "")String itime2){
        PageHelper.startPage(pageNum,2);
        Map map = new HashMap();
        map.put("itime1",itime1);
        map.put("itime2",itime2);
        List<Song> list =service.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("plist",pageInfo);
        model.addAttribute("itime1",itime1);
        model.addAttribute("itime2",itime2);
        return "list";
    }

    @RequestMapping("add.do")
    public String getadd(Song song, Integer[] gs,
                         @RequestParam("myFile")CommonsMultipartFile myFile,
                         HttpServletRequest request) throws IOException {
        //获取地址
        String realPath = request.getServletContext().getRealPath("uplog");
        //获取姓名
        String originalFilename = myFile.getOriginalFilename();
        File file = new File(realPath + "/" + originalFilename);
        myFile.transferTo(file);
        song.setPhoto("uplog/"+originalFilename);
        int addsong = service.addsong(song);
        if (addsong>0){
            for (int s :gs){
                Middle middle = new Middle();
                middle.setSid(song.getSid());
                middle.setCid(s);
                service.addmiddle(middle);
            }
        }
        return "redirect:list.do";
    }
    //歌手回显
    @RequestMapping("geshou.do")
    @ResponseBody
    public List<Singer>getgeshou(){
        List<Singer> sing = service.sing();
        return sing;
    }
    //删除
    @RequestMapping("del.do")
    @ResponseBody
    public int getdel(Integer[] sid){
        int delsong = service.delsong(sid);
        if (delsong>0){
            int delmiddle = service.delmiddle(sid);
            return delmiddle;
        }
        return 0;
    }
    //根据id查询
    @RequestMapping("idlist.do")
    @ResponseBody
    public Song getidlist(Integer sid){
       Song idlist = service.idlist(sid);
        return idlist;
    }
    //评分
    @RequestMapping("pf.do")
    public String getpf(Model model){
        List<Song> pf = service.pf();
        System.out.println(pf+"///////**///*/**/");
        model.addAttribute("pf",pf);
        return "redirect:list.do";
    }
    //下载
    @RequestMapping("xz.do")
    @ResponseBody
//    download(String filepath,
//             HttpServletRequest request,
//             HttpServletResponse response)
    public void getxz(String down,
                      HttpServletRequest request,
                      HttpServletResponse response) throws Exception {
        FileUtils.download(down,request,response);
    }
}
